package com.cykj.page02.servlet;

import com.cykj.page02.MyHttpRequest;
import com.cykj.page02.MyHttpResponse;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page02.servlet
 * @Author: 阿隆
 * @CreateTime: 2025-10-25 11:36
 * @Description: TODO
 * @Version: 1.0
 */
public class LoginServlet  extends BaseServlet{

    @Override
    public void doGet(MyHttpRequest httpRequest, MyHttpResponse httpResponse) {
        String account = httpRequest.getRequestParamToKey("account");
        String password = httpRequest.getRequestParamToKey("password");

        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //建立连接
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/mybatis_wql",
                    "root",
                    "root");

            //写Sql语句
            String sql = "select * from student where account = ?  and password = ?";

            //创建执行者对象
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, account);
            ps.setString(2, password);


            //执行sql语句并处理返回结果
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                System.out.println("登录成功");
                httpResponse.write("登录成功".getBytes());
            } else {
                System.out.println("登录失败");
                httpResponse.write("登录失败".getBytes());

            }

            //释放资源

            rs.close();
            ps.close();
            conn.close();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    @Override
    public void doPost(MyHttpRequest httpRequest, MyHttpResponse httpResponse) {
        doGet(httpRequest, httpResponse);
    }


}